OrdersController   A
last analyzed

Complexity

Total Complexity 3

Size/Duplication

Total Lines 25
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
wmc 3
eloc 21
dl 0
loc 25
c 0
b 0
f 0
rs 10

1 Function

Rating   Name   Duplication   Size   Complexity  
A order 0 23 3
1
import type { HttpContextContract } from '@ioc:Adonis/Core/HttpContext'
2
import OrderRequest from 'App/Requests/OrderRequest'
3
import I18n from '@ioc:Adonis/Addons/I18n'
4
import {
5
  addOrderToCollection,
6
  filterProductIDs,
7
  getProducts,
8
  getOrders
9
} from 'App/Services/OrderService'
10
11
export default class OrdersController {
12
  public async order({ auth, request }: HttpContextContract)
13
  {
14
    const payload = await OrderRequest.validate(request)
15
    if (payload.hasOwnProperty('errors')) {
16
      return payload
17
    }
18
19
    const collection = addOrderToCollection(payload['products'])
20
    const productIDs = await filterProductIDs(collection)
21
    const products = await getProducts(productIDs)
22
23
    if (products.length === 0) {
24
      return {
25
        success: false, error: true, code: 422,
26
        data: {
27
          message: I18n.locale('en').formatMessage('order.no_product')
28
        }
29
      }
30
    }
31
32
    return {
33
      success: true, error: false, code: 200,
34
      data: await getOrders(products, collection, auth)
35
    }
36
  }
37
}
38